function commons() {
    this.mainContainerH = $('.mian_cotainer').height();
    this.windowH = $(window).height();
    this.slider = $('.slider');
    this.treeList = $('.tree_list');
    this.treeSecondList = $('.tree_second_list>li');
    this.topCheck = $('.topCheckbox');
    this.bodyCheckbox = $('.bodyCheckbox');
    this.init();
}

commons.prototype = {
    init: function () {
        // this.slider.css('min-height', this.mainContainerH);
        this.bind();
    },
    bind: function () {
        var _this = this;
        this.treeList.on('mouseover', function (e) {
            var _self = $(this);
            _self.addClass('hover');
            e.stopPropagation();
        });
        this.treeSecondList.on('mouseover', function (e) {
            var _self = $(this);
            _self.addClass('hover');
            e.stopPropagation();
        });
        this.treeList.on('mouseout', function (e) {
            var _self = $(this);
            _self.removeClass('hover');
            e.stopPropagation();
        });
        this.treeSecondList.on('mouseout', function (e) {
            var _self = $(this);
            _self.removeClass('hover');
            e.stopPropagation();
        });
        this.topCheck.on('change', function () { //全选
            var _self = $(this);
            var type = _self.attr('checked');
            if (type === 'checked') {
                for (var i = 0; i < _this.bodyCheckbox.length; i++) {
                    _this.bodyCheckbox.eq(i).attr('checked', true);
                }
            } else {
                for (var j = 0; j < _this.bodyCheckbox.length; j++) {
                    _this.bodyCheckbox.eq(j).attr('checked', false);
                }
            }
        });

    },
    ajaxRequest: function (ajaxUrl, ajaxType, ajaxdataType, params, successCallback, errorCallBack) {
        $.ajax({
            url: ajaxUrl,
            type: ajaxType,
            dataType: ajaxdataType,
            data: params,
            success: function (data) {
                if (typeof successCallback === 'function') successCallback(data);
            },
            error: function (t, m, s) {
                if (errorCallBack && typeof errorCallBack === 'function') {
                    errorCallBack(t, m, s);
                }
            }
        });
    },
    getProvince: function (callback) { //获取省
        this.ajaxRequest('/address/getProvince', 'post', 'json', '', callback);
    },
    getCity: function (id, callback) { //获取市
        this.ajaxRequest('/address/getcity', 'post', 'json', {'pid': id}, callback);
    },
    getArea: function (id, callback) { //获取区
        this.ajaxRequest('/address/getarea', 'post', 'json', {'pid': id}, callback);
    },
    chooseArea: function (params) { //省市弹窗
        var _this = this;
        var types = params.types; //类型 1,省市；2省市区；3省市区海外 等等，方便扩展 ,此参数目前可以不传
        var callback = params.callback; //回调函数
        var idlist = {};
        var namelist = {}; //存放id 和 name
        var html = '';
        html += '<div class="modal_fixed" id="js-hall-modal_fixed">';
        html += '<div class="hall_choose_address">';
        html += '<a class="modal_close"></a>';
        html += '<ul class="hall_choose_btn" id="js_hall_choose_btn">';
        // if(type == 1){
        html += '<li class="active" id="js-province" data-type="province">';
        html += '<span>请选择</span>';
        html += '<img src="/statics/image/arrow_down.png" alt="">';
        html += '</li>';

        html += '<li class="" data-type="city" id="js-city">';
        html += '<span>请选择</span>';
        html += '<img src="/statics/image/arrow_down.png" alt="">';
        html += '</li>';

        html += '</ul>';
        html += '<div id="js_hall_show_address">';
        html += '<ul id="js_hall_show_address_p" class="hall_choose_content">';
        html += '</ul>';
        html += '<ul id="js_hall_show_address_c" class="hall_choose_content">';
        html += '</ul>';
        // }
        html += '</div>';
        html += '</div>';
        html += '</div>';
        _this.getProvince(function (data) {
            var phtml = '';
            for (var i = 0; i < data.length; i++) {
                phtml += '<li data-id="' + (data[i].provinceID || '') + '">' + (data[i].province || '') + '</li>';
            }
            $('#js_hall_show_address_p').html(phtml);
        });
        $('body').append(html);
        $('#js-hall-modal_fixed').css('display', 'block');
        $(document).off('click'); //解绑click事件
        $(document).on('click', '#js_hall_show_address_p>li', function () {
            var self = $(this);
            var provinceID = self.data('id');
            var province = self.html();
            _this.getCity(provinceID, function (dataList) {
                // console.log(dataList);
                var chtml = '';
                for (var i = 0; i < dataList.length; i++) {
                    chtml += '<li data-id="' + (dataList[i].cityID || '') + '">' + (dataList[i].city || '') + '</li>';
                }
                $('#js_hall_show_address_c').html(chtml);
                $('#js-province>span').html(province);
                $('#js-province').removeClass('active');
                $('#js-city').addClass('active');
                $('#js_hall_show_address_p').css('display', 'none');
                $('#js_hall_show_address_c').css('display', 'block');
                idlist['departProId'] = provinceID;
                namelist['departPro'] = province;
            });
            // console.log(that);
        });
        $(document).on('click', '#js-province', function () {
            $(this).addClass('active').siblings().removeClass('active');
            $('#js_hall_show_address_p').css('display', 'block');
            $('#js_hall_show_address_p').siblings().css('display', 'none');
            // console.log(that);
        });
        $(document).on('click', '#js-city', function () {
            $(this).addClass('active').siblings().removeClass('active');
            $('#js_hall_show_address_c').css('display', 'block');
            $('#js_hall_show_address_c').siblings().css('display', 'none');
            // console.log(that);
        });
        $(document).on('click', '#js_hall_show_address_c>li', function () {
            var self = $(this);
            var cityID = self.data('id');
            var city = self.html();
            idlist['departCityId'] = cityID;
            namelist['departCity'] = city;
            $('#js-city>span').html(city);
            // console.log(that);
            if ($('#js_hall_choose_btn>li').length === 2) {
                // that = null;
                callback(idlist, namelist);
                $('#js-hall-modal_fixed').remove();
            }
        });
        //close
        $(document).on('click', '.modal_close', function () {
            $('#js-hall-modal_fixed').remove();
        });
    },
    chooseArea_kz: function (params) {
        var _this = this;
        var types = params.types; //类型 1,省市；2省市区；3省市区海外 等等，方便扩展 ,此参数目前可以不传
        var callback = params.callback; //回调函数
        var idlist = {};
        var namelist = {}; //存放id 和 name
        var html = '';
        html += '<div class="modal_fixed" id="js-hall-modal_fixed">';
        html += '<div class="hall_choose_address">';
        html += '<a class="modal_close"></a>';
        html += '<ul class="hall_choose_btn" id="js_hall_choose_btn">';
        // if(type == 1){
        html += '<li class="active" id="js-province" data-type="province">';
        html += '<span>请选择</span>';
        html += '<img src="/statics/image/arrow_down.png" alt="">';
        html += '</li>';

        html += '<li class="" data-type="city" id="js-city">';
        html += '<span>请选择</span>';
        html += '<img src="/statics/image/arrow_down.png" alt="">';
        html += '</li>';

        html += '<li class="" data-type="town" id="js-town">';
        html += '<span>请选择</span>';
        html += '<img src="/statics/image/arrow_down.png" alt="">';
        html += '</li>';

        html += '</ul>';
        html += '<div id="js_hall_show_address">';
        html += '<ul id="js_hall_show_address_p" class="hall_choose_content">';
        html += '</ul>';
        html += '<ul id="js_hall_show_address_c" class="hall_choose_content">';
        html += '</ul>';
        html += '<ul id="js_hall_show_address_t" class="hall_choose_content">';
        html += '</ul>';
        // }
        html += '</div>';
        html += '</div>';
        html += '</div>';
        _this.getProvince(function (data) {
            var phtml = '';
            for (var i = 0; i < data.length; i++) {
                phtml += '<li data-id="' + (data[i].provinceID || '') + '">' + (data[i].province || '') + '</li>';
            }
            $('#js_hall_show_address_p').html(phtml);
        });
        $('body').append(html);
        $('#js-hall-modal_fixed').css('display', 'block');
        $(document).off('click'); //解绑click事件
        $(document).on('click', '#js_hall_show_address_p>li', function () {
            var self = $(this);
            var provinceID = self.data('id');
            var province = self.html();
            _this.getCity(provinceID, function (dataList) {
                // console.log(dataList);
                var chtml = '';
                for (var i = 0; i < dataList.length; i++) {
                    chtml += '<li data-id="' + (dataList[i].cityID || '') + '">' + (dataList[i].city || '') + '</li>';
                }
                $('#js_hall_show_address_c').html(chtml);
                $('#js-province>span').html(province);
                $('#js-province').removeClass('active');
                $('#js-city').addClass('active');
                $('#js_hall_show_address_p').css('display', 'none');
                $('#js_hall_show_address_c').css('display', 'block');
                idlist['departProId'] = provinceID;
                namelist['departPro'] = province;
            });
            // console.log(that);
        });
        $(document).on('click', '#js-province', function () {
            $(this).addClass('active').siblings().removeClass('active');
            $('#js_hall_show_address_p').css('display', 'block');
            $('#js_hall_show_address_p').siblings().css('display', 'none');
            // console.log(that);
        });
        $(document).on('click', '#js-city', function () {
            $(this).addClass('active').siblings().removeClass('active');
            $('#js_hall_show_address_c').css('display', 'block');
            $('#js_hall_show_address_c').siblings().css('display', 'none');
            // console.log(that);
        });
        $(document).on('click', '#js-town', function () {
            $(this).addClass('active').siblings().removeClass('active');
            $('#js_hall_show_address_t').css('display', 'block');
            $('#js_hall_show_address_t').siblings().css('display', 'none');
            // console.log(that);
        });


        $(document).on('click', '#js_hall_show_address_c>li', function () {
            var self = $(this);
            var cityID = self.data('id');
            var city = self.html();
            idlist['departCityId'] = cityID;
            namelist['departCity'] = city;
            $('#js-city>span').html(city);
            // console.log(that);
            if ($('#js_hall_choose_btn>li').length === 2) {
                // that = null;
                callback(idlist, namelist);
                $('#js-hall-modal_fixed').remove();
            } else {
                _this.getArea(cityID, function (dataList) {
                    var chtml = '';
                    for (var i = 0; i < dataList.length; i++) {
                        chtml += '<li data-id="' + (dataList[i].areaID || '') + '">' + (dataList[i].area || '') + '</li>';
                    }

                    $('#js-city').removeClass('active');
                    $('#js-town').addClass('active');
                    $('#js_hall_show_address_c').css('display', 'none');
                    $('#js_hall_show_address_t').html(chtml).css('display', 'block');
                });
            }
        });

        $(document).on('click', '#js_hall_show_address_t>li', function () {
            var self = $(this);
            var townID = self.data('id');
            var town = self.html();
            idlist['departTownId'] = townID;
            namelist['departTown'] = town;
            $('#js-town>span').html(town);
            // console.log(that);
            if ($('#js_hall_choose_btn>li').length === 3) {
                // that = null;
                console.log(idlist, namelist);
                callback(idlist, namelist);
                $('#js-hall-modal_fixed').remove();
            }
        });

        //close
        $(document).on('click', '.modal_close', function () {
            $('#js-hall-modal_fixed').remove();
        });
    },
    webfileUp: function (filedom, server_src, callback, tag) { //WebUploader 上传方法封装
        var ismultiple = tag || false;
        var uploader = WebUploader.create({
            pick: {
                id: filedom,
                multiple: ismultiple
            },
            accept: {
                title: 'Images',
                extensions: 'jpg,jpeg,bmp,png',
                mimeTypes: 'image/jpg,image/jpeg,image/png'
            },
            swf: '/statics/plugins/webuploader-0.1.5/Uploader.swf',
            server: server_src,
            auto: true
        });
        //上传成功
        uploader.on('uploadSuccess', function (file, data) {
            callback(file, data);
        });
        return uploader
    },
    showMessage: function (tips, callback) { //有确定按钮的消息提示框
        var html = '';
        var _h, _w;
        html += '<div class="modal_fixed" id="tips_modal">';
        html += '<div class="tips_container">';
        html += '<a class="modal_close"></a>';
        html += '<div class="tips_detail">' + (tips || '') + '</div>';
        html += '<div class="modal_bottom_btn"><input type="button" value="确定" class="bottom_ok normal_btn save_send"></div>';
        html += '</div>';
        html += '</div>';
        $('body').append(html);
        $('#tips_modal').css('display', 'block');
        _h = $('.tips_container').height();
        _w = $('.tips_container').width();
        $('.tips_container').css({
            'margin-top': -(_h / 2) + 'px',
            'margin-left': -(_w / 2) + 'px'
        });
        if (typeof callback === 'function') {
            // $(document).off('click');
            $('.bottom_ok').click(function () {
                callback();
            });
        } else {
            // $(document).off('click');
            $('.bottom_ok').click(function () {
                $('#tips_modal').remove();
            });
        }
        $('.modal_close').click(function () {
            $('#tips_modal').remove();
        });
    },
    fixMargin: function (dom) {
        dom.css({
            'margin-left': -dom.width() / 2,
            'margin-top': -dom.height() / 2
        })
    },
    //提交之前验证
    beforeSubmit: function (obj) {
        var self = this;
        var oli_list = obj.find('.require');
        if (oli_list.length < 1) {
            oli_list = obj.find('.required');
        }
        var onOff = true;
        console.log(oli_list.length);
        $.each(oli_list, function (index, value) {
            var require_obj_input = $(value).find('input');
            var require_obj_sel = $(value).find('select');
            var name = $(value).find('span').html();
            if (name.indexOf("：") !== -1) {
                name = name.replace('：', '');
            }
            if (name.indexOf(":") !== -1) {
                name = name.replace(':', '');
            }

            if (require_obj_sel.val() === 0 || require_obj_sel.val() === '请选择') {
                self.showMessage('请填写：' + name);
                onOff = false;
                return false
            }
            if (require_obj_input.val() === '' || require_obj_input.val() === '请选择') {
                self.showMessage('请填写：' + name);
                onOff = false;
                return false
            }
        });
        return onOff;
    },
    upload_person_img: function (obj, w, h) {/*obj为需要缩放图片的集合*/
        var ow, oh;
        $.each(obj, function (index, value) {
            var oimg = $(value).find('img');
            var obj_ow = oimg.closest('div').width();
            var obj_oh = oimg.closest('div').height();

            if (w == undefined) {
                ow = oimg.width();
                oh = oimg.height();
            } else {
                ow = w;
                oh = h;
            }

            if (ow > oh) {
                oimg.css({
                    'width': '100%',
                    'height': 'auto'
                })
            } else {
                oimg.css({
                    'height': '100%',
                    'width': 'auto'
                })
            }
        })
    },

    time_check: function (f_time, s_time) {
        var self = this;
        if (s_time == undefined) {
            s_time = new Date().getTime()
        } else {
            s_time = change_time(s_time);
        }

        function change_time(t) {
            var time = t.split(' ');
            var d = time[0].split('-');
            var h = time[1].split(':');
            var y = d[0], m = d[1], day = d[2];
            var hour = h[0], min = h[1];
            var date = new Date(y, m - 1, day, hour, min);
            var check_time = date.getTime();
            return check_time;
        }

        f_time = change_time(f_time);
        return (f_time < s_time)
    },
    showMessageTop: function (tips) {
        if (tips == undefined) {
            tips = "操作成功";
        }
        var odiv = $('<div  class="top_show_message" id="js_top_show_message">' + tips + '</div>');
        $('body').append(odiv);
        // odiv.animate({top: '28px'}, 200, 'linear');
        clearTimeout(timer);
        var timer = setTimeout(function () {
            odiv.animate({opacity: 0}, 500, 'linear', function () {
                odiv.remove();
            });
            clearTimeout(timer);
        }, 3000);

    }

}
;

function showMessage(tips, callback) { //后台能调用的showMessage
    var html = '';
    var _h, _w;
    html += '<div class="modal_fixed" id="tips_modal">';
    html += '<div class="tips_container">';
    html += '<a class="modal_close"></a>';
    html += '<div class="tips_detail">' + (tips || '') + '</div>';
    html += '<div class="modal_bottom_btn"><input type="button" value="确定" class="bottom_ok normal_btn save_send"></div>';
    html += '</div>';
    html += '</div>';
    $('body').append(html);
    $('#tips_modal').css('display', 'block');
    _h = $('.tips_container').height();
    _w = $('.tips_container').width();
    $('.tips_container').css({
        'margin-top': -(_h / 2) + 'px',
        'margin-left': -(_w / 2) + 'px'
    });
    if (typeof callback === 'function') {
        // $(document).off('click');
        $('.bottom_ok').click(function () {
            callback();
        });
    } else {
        // $(document).off('click');
        $('.bottom_ok').click(function () {
            $('#tips_modal').remove();
        });
    }
    $('.modal_close').click(function () {
        $('#tips_modal').remove();
    });
}

function showMessageTop(tips) { //后台调用的showMessageTop顶部成功弹窗
    if (tips == undefined) {
        tips = "操作成功";
    }
    var odiv = $('<div  class="top_show_message" id="js_top_show_message">' + tips + '</div>');
    $('body').append(odiv);

    clearTimeout(timer);
    var timer = setTimeout(function () {
        odiv.animate({opacity: 0}, 500, 'linear', function () {
            odiv.remove();
        });
        clearTimeout(timer);
    }, 3000);
}


function showMessageConfirm(tips, url) { //后台能调用的showMessage
    var html = '';
    var _h, _w;
    html += '<div class="modal_fixed" id="tips_modal">';
    html += '<div class="tips_container">';
    html += '<a class="modal_close"></a>';
    html += '<div class="tips_detail">' + (tips || '') + '</div>';
    html += '<div class="modal_bottom_btn"><input type="button" value="取消" class="bottom_off normal_btn save_send"><input type="button" value="确定" class="bottom_ok normal_btn save_send"></div>';
    html += '</div>';
    html += '</div>';
    $('body').append(html);
    $('#tips_modal').css('display', 'block');
    _h = $('.tips_container').height();
    _w = $('.tips_container').width();
    $('.tips_container').css({
        'margin-top': -(_h / 2) + 'px',
        'margin-left': -(_w / 2) + 'px'
    });

    $('.bottom_ok').click(function () {
        window.location.href = url;

    });
    $('.bottom_off').click(function () {
        $('#tips_modal').remove();
    });

    $('.modal_close').click(function () {
        $('#tips_modal').remove();
    });
}

function showNewsInfo(tips_l) {
    var str = '';
    var str_desktop = '';
    tips_l.map(function (data) {
        str += '<p>' + data['content'] + '</p>\n';
        str_desktop += data['content'] + '\n';
    });
    console.log(str);
    var odiv = $('<div class="top_news_info">\n' +
        '<p class="news_info_tips">\n' +
        '<span>消息</span>\n' +
        '<a href="/message/list">查看更多</a>\n' +
        '</p>\n' +
        '<div class="news_info_content">\n' +
        '<p>系统消息</p>\n' +
        str +
        '</div>\n' +
        '</div>');
    //系统通知
    if (window.Notification) {
        if (window.Notification.permission !== 'granted' || Notification.permission !== true) {
            window.Notification.requestPermission();//请求通知
        }

        if (Notification.permission === 'granted' || Notification.permission === true) {
            var str = '';
            var notification = new Notification('系统消息', {body: str_desktop});
            //5S关闭或者点击关闭
            clearTimeout(timer);
            var timer=setTimeout(function () {
                notification.close();
                alert(1);
            },5000);
            notification.onclick=function () {
                notification.close();
            };
        } else {
            alert('未被允许桌面通知')
        }
    } else {
        alert('你的浏览器不支持桌面通知，请下载谷歌浏览器使用该功能');
    }
    //页面通知
    // $('body').append(odiv);

    clearTimeout(timer);
    var timer = setTimeout(function () {
        odiv.animate({opacity: 0}, 500, 'linear', function () {
            odiv.remove();
        });
        clearTimeout(timer);
    }, 5000);
}

function getNewsInfo() {
    $.ajax({
        'url': '/message/newmsg',
        type: "get",
        dataType: 'json',
        success: function (data) {
            if (data['errno'] == 0) {
                if (data['result'].length > 0) {
                    // console.log(data['result']);
                    showNewsInfo(data['result']);
                }
            } else {
                showMessage(data['errmsg']);
            }
        }
    })
}


$(function () {
    var cm = new commons();

});
